//
//  NSString+getDateTime.m
//  ITMS
//
//  Created by 中矿科技有限公司 on 16/7/18.
//  Copyright © 2016年 中矿科技有限公司. All rights reserved.
//

#import "NSString+getDateTime.h"

@implementation NSString (getDateTime)


+(void)getWeekTime:(weekDate)weekday
{
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
    NSLog(@"%ld----%ld",(long)weekDay,(long)day);
    
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1)
    {
        firstDiff = -6;
        lastDiff = 0;
    }
    else
    {
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
   // NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd "];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
   // NSLog(@"%@",firstDay);
    weekday(firstDay,lastDay);
}
+(void)getMothTime:(mothDate)mothday
{
    
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
   // NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [firstDayComp setDay:1];//当月第一天
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    //    当前时间对应的月份中有几天
   NSInteger mothNumber =[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]].length;
    [firstDayComp setDay:mothNumber];//设置当月最后一天
    NSDate *lastDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
        mothday(firstDay,lastDay);
}

+(void)getYearStatus:(SAPYearStatus)yearStatus yearTime:(mothDate)mothday
{
    
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    if (yearStatus ==SAPCurrentYear ) {
        [firstDayComp setDay:1];
        [firstDayComp setMonth:1];//今年第一天
    }else if (yearStatus ==SAPCurrentLastYear){
        [firstDayComp setDay:1];
        [firstDayComp setMonth:1];
        [firstDayComp setYear:[firstDayComp year]-1];
    }else{
        [firstDayComp setDay:1];
        [firstDayComp setMonth:1];
        [firstDayComp setYear:[firstDayComp year]-2];
    }

    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    //    当前时间对应的月份中有几天
    NSInteger mothNumber =[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:[NSDate date]].length;
    
    if (yearStatus ==SAPCurrentYear || yearStatus ==SAPCurrentLastYear) {
        [firstDayComp setDay:31];
        [firstDayComp setMonth:mothNumber];//今年第一天
    }else if (yearStatus == SAPNearThreeYear){
        
        [firstDayComp setDay:31];
        [firstDayComp setMonth:1];
        [firstDayComp setYear:[firstDayComp year]+2];
    }
    
    [firstDayComp setMonth:mothNumber];//设置当月最后一天
    NSDate *lastDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
    mothday(firstDay,lastDay);
}

-(CGFloat)heightWithWidth:(CGFloat)width font:(CGFloat)font{
    UIFont * fonts = [UIFont systemFontOfSize:font];
    CGSize size  = CGSizeMake(width, 100000.0);
    NSDictionary * dict  = [NSDictionary dictionaryWithObjectsAndKeys:fonts,NSFontAttributeName ,nil];
    size = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size.height;
}


@end
